package com.hz.filemanager.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hz.filemanager.R;
import com.hz.filemanager.model.clean.ResultBean;

import java.util.ArrayList;
import java.util.List;

public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ViewHolder>{

    private List<ResultBean> datas = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private ViewHolder holder;
    private int position;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setDatas(List<ResultBean> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder,  int position) {
        this.holder = holder;
        this.position = position;

        ResultBean resultBean = datas.get(position);
        holder.iconIv.setImageResource(resultBean.getIconid());
        holder.nameTv.setText(resultBean.getName());
        holder.btnTv.setText(resultBean.getBtn());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        private final ImageView iconIv;
        private final TextView nameTv;
        private final TextView btnTv;
        private final View itemView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iconIv = itemView.findViewById(R.id.icon_iv);
            nameTv = itemView.findViewById(R.id.name_tv);
            btnTv = itemView.findViewById(R.id.btn_tv);
            this.itemView = itemView;

        }
    }

    public interface OnItemClickListener{

        void onItemClick(int position);
    }

}
